// pages/studyWord/independentreview/independentreview.js
const app = getApp();
const httpUtils = require('../../../utils/httpUtils.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    imageurl: app.globalData.imageurl,
    okNums: 0,
    errNums: 0,
    selNums: {},
    ansNums: {},
    innerAudioContext: new Object(),
    allWord: '',
    canWord:[], //累计会的单词
    errWord: [],//累计不会的词
    can_notString: "",//不会的词的id字符串
    questData: [],
    wordID: 0,
    wordLen: 0,
    readTimes: {},
    packageid: "",
    page: 1,//分页 第一页是1
    pageSize: 20,
    hasMoreData: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that =this;
    this.data.innerAudioContext = wx.createInnerAudioContext()
    this.data.innerAudioContext.autoplay = false;
    this.data.innerAudioContext.loop = false;
    //获取到传过来的包id
    this.data.packageid = options.packageid;
    that.getReviewWords();
  },

 //获取自主复习的单词
  getReviewWords:function(){
    let that = this;
    wx.showLoading({
      title: '',
      mask: true
    })
    httpUtils.getIndpendentreviewWords({
      access_token: app.globalData.studentInfo.access_token ? app.globalData.studentInfo.access_token : app.globalData.access_token,
      package_id: that.data.packageid,
      page: that.data.page,
    }).then((res) =>{
      var questData = that.data.questData;
      var list = res.data.to_study;
      if (that.data.page == 1) {
        questData = []
      }
      if (list.length < that.data.pageSize) {
        that.setData({
          questData: questData.concat(list),
          hasMoreData: false
        })
      } else {
        that.setData({
          questData: questData.concat(list),
          hasMoreData: true,
          page: that.data.page + 1,
        })
      }

    });
  },

  /**
 * 提交复习的词
 */
submitData: function () {
  let that = this;
    //单词id以，隔开的字符串
    var canString = "";
    var can_notString = "";
    console.log("ansNum:"+JSON.stringify(this.data.ansNums))
 
    for (var key in that.data.ansNums) {
      if (that.data.ansNums[key] == 1) {
        if (key < that.data.questData.length) {
          canString += that.data.questData[key].id+",";
        }
      }else if(that.data.ansNums[key] == 2){
        if (key < that.data.questData.length) {
          can_notString += that.data.questData[key].id+",";
        }
      }
    }
    console.log("canString:" + canString);
    console.log("notcanstring:" + can_notString)
    wx.showLoading({
      title: '',
      mask: true
    })
    httpUtils.referWord({
      access_token: app.globalData.studentInfo.access_token ? app.globalData.studentInfo.access_token : app.globalData.access_token,
      package_id: this.data.packageid,
      type: "review",
      times: "2",
      f_type: "1",
      can: canString,
      can_not: can_notString,
    }).then((res) => {
      console.log(res);
      wx.showToast({
        title: "提交成功",
        icon: 'success',
        duration: 1000
      });
      setTimeout(function () {
        wx.navigateBack({
          delta: 1
        })
      }, 1000)

    });
  
},

clickErr: function (e) {
  let temWrod = e.currentTarget.dataset.words;
  let temA = this.data.ansNums;
  let errNums = this.data.errNums;
  let okNums = this.data.okNums;
  if (temA[temWrod] == 1) {
    okNums--;
  }
  if (temA[temWrod] != 2) {
    errNums++;
  }
  temA[temWrod] = 2;

  this.setData({
    ansNums: temA,
    errNums: errNums,
    okNums: okNums
  })
},
clickOk: function (e) {
  // console.log("-----------------", JSON.stringify(e));
  let temWrod = e.currentTarget.dataset.words;
  let errNums = this.data.errNums;
  let okNums = this.data.okNums;
  let temA = this.data.ansNums;
  if (temA[temWrod] == 2) {
    errNums--;
  }

  if (temA[temWrod] != 1) {
    okNums++;
  }

  temA[temWrod] = 1;
  this.setData({
    ansNums: temA,
    errNums: errNums,
    okNums: okNums
  })

},

clickTxt: function (e) {
  this.data.innerAudioContext.stop();
  let temWrod = e.currentTarget.dataset.words;
  let temNum = this.data.selNums;
  let audiosrc=e.currentTarget.dataset.audios;
  this.data.innerAudioContext.src = audiosrc;
  this.data.innerAudioContext.play();
  var isHas = false;//是否点击过 false没有点击过
  for (var key in temNum) {//0正常样式1英文变粗 2英文变粗出中文
    if (key == temWrod) {
      if (temNum[key] < 2) {
        temNum[key]++;
      } else {
        temNum[key] = 1;
      }
      isHas = true;
    } else {
      temNum[key] = 0;
    }
  }
  if (!isHas) {
    temNum[temWrod] = 1;
  }
  this.setData({
    selNums: temNum,
    // readTimes: rTimes
  });
},


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    console.log("onReachBottom方法触发了")
    var that = this;
    if (that.data.hasMoreData) {
      that.getReviewWords();
    } else {
      wx.showToast({
        title: '没有更多数据了',
        icon: 'none'
      })
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})